//
//  MapViewController.swift
//  bluetooth
//
//  Created by Evan on 2020/10/20.
//  Copyright © 2020 Evan. All rights reserved.
//

import Foundation
import CoreLocation

class MapViewController: UIViewController, BMKMapViewDelegate,BMKLocationAuthDelegate {
    
    var mapView:BMKMapView?
    var locationManager:BMKLocationManager!
    /// 定位获取的地址
    var curlocation:CLLocation?
    var annotation:BMKPointAnnotation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "地图扫描"
        BMKLocationAuth.sharedInstance()!.checkPermision(withKey: "GubD2oDobQN2HDaDCpL9BD1d9dK7b0sw", authDelegate: self)
        
        mapView = BMKMapView(frame: self.view.frame)
        mapView?.delegate = self
        mapView?.zoomLevel = 17
        mapView?.showMapScaleBar = true
        mapView?.showsUserLocation = true
        mapView?.userTrackingMode = BMKUserTrackingModeNone
        self.view.addSubview(mapView!);
        
        locationInit()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        mapView?.viewWillAppear()
    }
        
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        mapView?.viewWillDisappear()
    }
}

// 定位
extension MapViewController:BMKLocationManagerDelegate{
    
    func locationInit(){
        
        let btn = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 200, y: UIScreen.main.bounds.height - 100, width: 100, height: 50))
        btn.setTitle("获取定位", for: .normal)
        btn.backgroundColor = .red
        btn.addTarget(self, action: #selector(btnlocaltionClick), for: .touchUpInside)
        self.view.addSubview(btn)
        
        locationManager = BMKLocationManager()
        locationManager.delegate = self
        locationManager.coordinateType = .BMK09LL
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.activityType = .automotiveNavigation;
        locationManager.pausesLocationUpdatesAutomatically = false;
        locationManager.allowsBackgroundLocationUpdates = false;
        locationManager.locationTimeout = 10;
        locationManager.reGeocodeTimeout = 10;
        
        btnlocaltionClick(btn)
    }
    
    @objc func btnlocaltionClick(_ sender:UIButton){
        
        
        locationManager.requestLocation(withReGeocode: true, withNetworkState: true) { (location, state, error) in
            //获取经纬度和该定位点对应的位置信息
            if let l = location?.location?.coordinate {
                // 设置地图中心点
                self.mapView?.setCenter(l, animated: true)
                
               
                //初始化标注类BMKPointAnnotation的实例
                self.annotation = BMKPointAnnotation()
                //设置标注的经纬度坐标
                self.annotation?.coordinate = l
                //设置标注的标题
                self.annotation?.title = "当前位置"
                //副标题
                self.annotation?.subtitle = location?.rgcData?.locationDescribe// "天安门"
                /**
                 
                 当前地图添加标注，需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:方法
                 来生成标注对应的View
                 @param annotation 要添加的标注
                 */
                self.mapView?.addAnnotation(self.annotation!)
            }
        }
        
    }
    
    
    
    
//
//    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//        // 获取最新坐标
//        curlocation = locations.last
//        // 纬度
//        let w = curlocation?.coordinate.latitude ?? 0
//        // 经度
//        let j = curlocation?.coordinate.longitude ?? 0
//        let s = "定位地址： lat:\(w ??  0)  long:\(j ?? 0)"
//        print(s)
//
//
//        let coodinate = CLLocationCoordinate2DMake(w, j)
//        let cood = BMKLocationManager.bmkLocationCoordinateConvert(coodinate, srcType: .BMK09LL, desType: .BMK09MC)
//
//        //初始化标注类BMKPointAnnotation的实例
//        annotation = BMKPointAnnotation()
//        //设置标注的经纬度坐标
//        annotation?.coordinate = CLLocationCoordinate2D(latitude:cood.latitude, longitude: cood.longitude)
//        //设置标注的标题
//        annotation?.title = "当前位置"
//        //副标题
//        annotation?.subtitle = "天安门"
//        /**
//
//         当前地图添加标注，需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:方法
//         来生成标注对应的View
//         @param annotation 要添加的标注
//         */
//        mapView?.addAnnotation(annotation)
//
//    }
//
//    func mapView(_ mapView: BMKMapView!, viewFor annotation: BMKAnnotation!) -> BMKAnnotationView! {
//        if annotation.isKind(of: BMKPointAnnotation.self) {
//            /**
//             根据指定标识查找一个可被复用的标注，用此方法来代替新创建一个标注，返回可被复用的标注
//             */
//            var annotationView: BMKPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationViewIdentifier") as? BMKPinAnnotationView
//            if annotationView == nil {
//                /**
//                 初始化并返回一个annotationView
//
//                 @param annotation 关联的annotation对象
//                 @param reuseIdentifier 如果要重用view，传入一个字符串，否则设为nil，建议重用view
//                 @return 初始化成功则返回annotationView，否则返回nil
//                 */
//                annotationView = BMKPinAnnotationView.init(annotation: annotation, reuseIdentifier: "annotationViewIdentifier")
//            }
//            return annotationView
//        }
//        return nil
//    }
    
}
